PANEL = {}
PANEL.TimeDelay = 0.25
PANEL.Font = "Default"

function PANEL:Init()
	self.Id = 0

	self.OpenTime = 0
	self.MoveDistance = 200
	self.MoveAngle = 0
	
	self.Text = "TODO!"
	self.Active = false
	
	self.BaseClass.Init( self )
end

function PANEL:SetAngle( ang )
	self.MoveAngle = math.Deg2Rad( ang )
end

function PANEL:SetId( id )

	local Item = BUILDING.List[ id ]
	
	if !Item then	
		return
	end
	
	self.Text = Item.name
	self:SetModel( Item.model )
end

function PANEL:SetActive( state )
	self.Active = state
end

function PANEL:PerformLayout()

	self:SetSize( 128, 128 )

	if IsValid( self.Entity ) then
		local RenderMin, RenderMax = self.Entity:GetRenderBounds()
		local EntitySize = RenderMin:Length() + RenderMax:Length()
				
		self:SetLookAt( Vector(0,0,0) )
		self:SetCamPos( Vector(1,0,1) * EntitySize )
	end
	
end

function PANEL:Paint()
	
	if self.Active then
		surface.SetDrawColor( 125, 125, 125, 255 )
	else
		surface.SetDrawColor( 64, 64, 64, 255 )
	end
	
	surface.DrawRect( 0,0, self:GetWide(), self:GetTall() )
	
	surface.SetDrawColor( 255, 255, 255, 255 )
	surface.DrawOutlinedRect( 0,0, self:GetWide(), self:GetTall() )
	
	self.BaseClass.Paint( self )
	
	surface.SetFont( self.Font )
	surface.SetTextColor( 255,255,255,255 )
	
	local w, h = self:GetTextSize()
	
	surface.SetTextPos( self:GetWide() / 2 - w / 2, self:GetTall() - h - 5 )
	surface.DrawText( self.Text )
	
end

function PANEL:GetTextSize()
	surface.SetFont( self.Font )
	
	return surface.GetTextSize( self.Text )
end

function PANEL:Open()

	self.OpenTime = CurTime() + self.TimeDelay
	self.Think = self.ChagingThink
	self.Opening = true
	self:SetVisible( true )
	
end

function PANEL:Close()

	self.OpenTime = CurTime() + self.TimeDelay
	self.Think = self.ChagingThink
	self.Opening = false
	
end

function PANEL:ThinkFinished( time )

	if self.Opening then
		return time >= 1
	else
		return time <= 0
	end

end

function PANEL:ChagingThink()
	
	local TimeLeft = math.max( self.OpenTime - CurTime(), 0.0 ) / self.TimeDelay
	
	if self.Opening then
		TimeLeft = 1 - TimeLeft
	end
	
	local NewAngle = self.MoveAngle + TimeLeft * 0.1
	
	self:SetPos(
		ScrW() * 0.5 + math.cos( NewAngle ) * self.MoveDistance * TimeLeft - self:GetWide() / 2,
		ScrH() * 0.5 + math.sin( NewAngle ) * self.MoveDistance * TimeLeft - self:GetTall() / 2
	)
	
	self:SetAlpha( 255 * TimeLeft )
	
	if IsValid( self.Entity ) then
		self.Entity:SetColor( 255,255,255, 255 * TimeLeft )
	end
	
	if self:ThinkFinished( TimeLeft ) then
		self.Think = function() end
		
		if self.Opening == false then
			self:SetVisible( false )
		end
		
		return
	end
	
end

vgui.Register("BuildItem", PANEL, "DModelPanel")